package com.ringtheweb.web.serviceprovider.weather.action;

import java.io.Serializable;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.faces.Converter;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import com.ringtheweb.serviceprovider.weather.dao.ILocationDAO;
import com.ringtheweb.serviceprovider.weather.model.Location;
import com.ringtheweb.serviceprovider.weather.model.LocationType;
import com.ringtheweb.serviceprovider.weather.model.TemperatureUnit;
import com.ringtheweb.serviceprovider.weather.model.WeatherPlay;
import com.ringtheweb.web.action.PlayListAction;

@Name("weather")
@Scope(ScopeType.PAGE)
@Converter
public class WeatherAction implements Serializable,javax.faces.convert.Converter {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	private static final TemperatureUnit DEFAULT_TEMP_UNIT = TemperatureUnit.CELCIUS;
	
	@In FacesMessages facesMessages;
	
	@Logger	Log LOG;
	
	private @In("#{playListAction}") PlayListAction playListAction;
	
	private @In("#{locationDAO}") ILocationDAO locationDAO;
	
	private Location location;
	
	private TemperatureUnit tempUnit = DEFAULT_TEMP_UNIT;
	
	private static final String LOCATION_SEP=",";
	
	
	public String getLocationSeparator() {
		return LOCATION_SEP;
	}
	
	public TemperatureUnit getTempUnit() {
		return tempUnit;
	}

	public void setTempUnit(TemperatureUnit tempUnit) {
		this.tempUnit = tempUnit;
	}
	
	
	
	public List<Location> autoComplete(Object event) {
		String text = (String)event;
		return locationDAO.findByTypeAndApprochingName(LocationType.CITY, text);
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}
	
	public String addWeatherPlay() {
		WeatherPlay weatherPlay = new WeatherPlay() ;
		weatherPlay.setLocation(location);
		weatherPlay.setTempUnit(tempUnit);
		playListAction.getPlays().add(weatherPlay);
		location = null;
		tempUnit = DEFAULT_TEMP_UNIT;
		return "";
	}

	/**
	 * Location conversion
	 */
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		String[] frags = value.split(LOCATION_SEP);
		if (frags != null && frags.length == 2) {
			String cityName = frags[0];
			String stateName = frags[1];
			Location location = locationDAO.findByNameAndParentName(cityName.trim(), stateName.trim(), 
					LocationType.CITY);
			return location;
		}
		
		return null;
		
//			FacesMessage facesMessage = FacesMessages.createFacesMessage(FacesMessage.SEVERITY_ERROR,
//					"error_conversion_location", "");
//			throw new ConverterException(facesMessage);
		
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		
		Location location = (Location)value;
		return location.getName();
	}
	
	
	

}
